const CompressionPlugin = require("compression-webpack-plugin"),
	path = require("path"),
	envConfig = require("./src/assets/config");

module.exports = {
	publicPath: "/pc",
	productionSourceMap: false,
	chainWebpack: (config) => {
		config.resolve.alias
			.set("@", resolve("src"))
			.set("@assets", resolve("src/assets"))
			.set("@components", resolve("src/components"))
			.set("@router", resolve("src/router"))
			.set("@store", resolve("src/store"))
			.set("@views", resolve("src/views"));
		config.plugin("define").tap((args) => {
			for (const key in envConfig) {
				args[0]["process.env"][key] = JSON.stringify(envConfig[key]); // 字符串化
			}
			return args;
		});
	},
	configureWebpack: () => {
		if (process.env.NODE_ENV === "production") {
			return {
				plugins: [
					new CompressionPlugin({
						test: /\.js$|\.html$|.\css/, // 匹配文件名
						threshold: 10240, // 对超过10k的数据压缩
						deleteOriginalAssets: false, // 不删除源文件
					}),
				],
			};
		}
	},
	lintOnSave: false,
	devServer: {
		proxy: {
			"/admin": {
				target: envConfig.ORIGIN_URL,
				changeOrigin: true, // 允许跨域
				pathRewrite: {
					"^/admin": "/",
				},
			},
		},
	},
};

function resolve(dir) {
	return path.join(__dirname, dir);
}
